package com.brook.utils;

import com.sun.org.apache.regexp.internal.RE;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.crypto.Data;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;

/**
 * @author Brook
 */

public class JwtUtil {

    //有效期为
    public static final Long JWT_TTL = 60 * 60 * 1000L;

    //设置密钥铭文
    public static final String JWT_KEY = "saneng";

    public static String getUUID() {
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        return token;
    }

    /**
     * 生成jtw
     */
    public static String createJWT(String subject) {
        //设置过期时间
        JwtBuilder builder = getJwtBuilder(subject, null, getUUID());
        return builder.compact();
    }

    private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        SecretKey secretKey = generalKey();
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        if (ttlMillis == null) {
            ttlMillis=JwtUtil.JWT_TTL;
        }
        long expMillis = nowMillis + ttlMillis;
        Date expDate = new Date(expMillis);
        return Jwts.builder()
                .setId(uuid)     //唯一的ID
                .setSubject(subject) //主题，可以是JSON数据
                .setIssuer("sg")  //签发者
                .setIssuedAt(now)  //签发时间
                .signWith(signatureAlgorithm, secretKey)  //使用HS256对称加密算法签名，第二个参数为密钥
                .setExpiration(expDate);
    }

    /**
     * 创建token
     * @return
     */
    public static String createJWT(String id, String subject, Long ttMillis) {
        JwtBuilder builder = getJwtBuilder(subject, ttMillis, id);
        return builder.compact();
    }

    public static void main(String[] args) {
        System.out.println(createJWT("123456"));
        System.out.println(parseJWT("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJiOTkyZmZiZDVmMzE0YzdjOWEzNmQwZDY3MjZkNjMzMyIsInN1YiI6IjEyMzQ1NiIsImlzcyI6InNnIiwiaWF0IjoxNjg4NjA2NzQ0LCJleHAiOjE2ODg2MTAzNDR9.BNzMLvUAOUDbo-jkjliz1CvfI3MZRZDNE6oop6LzY5U").getSubject());

        System.out.println(parseJWT("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhNmY2MDdhMjQzYjY0Njc2YTg4NGM3MjVmYjYzNTZkNSIsInN1YiI6IjEiLCJpc3MiOiJzZyIsImlhdCI6MTY4ODYxNDg5NywiZXhwIjoxNjg4NjE4NDk3fQ.3tQ_DT54Q9-lkA0EEacajV9cCYW5fUFVXgbF6s_0rlU").getSubject());
    }

    /**
     * 生成加密后的密钥
     * @return
     */
    public static SecretKey generalKey() {
        byte[] encodeKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
        SecretKey key = new SecretKeySpec(encodeKey, 0, encodeKey.length, "AES");
        return key;
    }

    /**
     * 解析
     *
     */
    public static Claims parseJWT(String jwt) {
        SecretKey secretKey = generalKey();
        return Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(jwt)
                .getBody();
    }

}
